package com.intelligentmeterreadingsys.waterbanding.db;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.intelligentmeterreadingsys.BaseDao.BaseDao;
import com.intelligentmeterreadingsys.povo.UserWaterVO;
import com.intelligentmeterreadingsys.povo.WaterInfoVO;

import dbhelper.DbTableName;

/**
 * 水表绑定实现类
 * 
 * @author king
 * 
 */
public class WaterBandingImpl extends BaseDao implements IWaterBanding {

	private SQLiteDatabase db;

	public WaterBandingImpl(Context context) {
		super(context);
		this.db = super.getDb();

	}

	/**
	 * 查询水表信息
	 */
	@Override
	public UserWaterVO queryWaterInfo(String waterid) {
		StringBuffer sb = new StringBuffer(
				"select w.[waternumber],w.[adds],w.[_id] as waterid,w.[usernumber],w.[wateradds],w.[waterproperties],w.[RFIDnumber]");
		sb.append(",w.[latitude],w.[longitude]");
		sb.append(",u.[username],u.[phone],u.[usertype] from waterinfo w,userinfo u where w.[usernumber]=u.[usernumber]");

		if (waterid != null && !waterid.equals("")) {
			sb.append(" and w.[_id]=").append(waterid);

		} else {
			return null;
		}

		String sql = sb.toString();

		System.out.println(sql);
		Cursor cursor = db.rawQuery(sql, null);

		if (cursor == null) {
			return null;
		}
		UserWaterVO water = null;
		if (cursor.moveToNext()) {
			water = new UserWaterVO();
			water.setWateradds(cursor.getString(cursor
					.getColumnIndex("wateradds")));
			water.setName(cursor.getString(cursor.getColumnIndex("username")));
			water.setNumber(cursor.getString(cursor
					.getColumnIndex("usernumber")));
			water.setWaternumber(cursor.getString(cursor
					.getColumnIndex("waternumber")));
			water.setRFIDnumber(cursor.getString(cursor
					.getColumnIndex("RFIDnumber")));
			water.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
			water.setLatitude(cursor.getDouble(cursor
					.getColumnIndex("latitude")));
			water.setLongitude(cursor.getDouble(cursor
					.getColumnIndex("longitude")));
			water.setAdds(cursor.getString(cursor.getColumnIndex("adds")));

			water.setWaterid(cursor.getString(cursor.getColumnIndex("waterid")));

		}
		if (cursor != null) {
			cursor.close();
		}
		return water;
	}

	/**
	 * 关闭连接
	 */
	public void close() {
		super.close();
	}

	/**
	 * 绑定水表
	 */
	@Override
	public boolean BandingRFID(String waternumber, String RFID,
			Double latitude, Double longitude, String adds) {
		if (TextUtils.isEmpty(waternumber)) {
			return false;
		}
		boolean b = false;
		ContentValues value = new ContentValues();

		if (RFID != null && RFID.length() > 0) {

			value.put("RFIDnumber", RFID);

		}
		if (latitude != null && latitude > 0) {
			System.out.println("添加");
			value.put("latitude", latitude);
			value.put("longitude", longitude);

		}

		value.put("adds", adds);
		value.put("bandingstatus", "0");

		int k = db.update("waterinfo", value, "_id=?",
				new String[] { waternumber });
		if (k > 0) {
			b = true;
		}

		return b;
	}

	/**
	 * 查询所有签名数
	 */
	@Override
	public int queryBandingTotal() {
		String sql = "select _id from waterinfo where bandingstatus='0' or bandingstatus='1' and ismultiplecopy='0'";
		System.out.println(sql);
		Cursor cursor = db.rawQuery(sql, null);
		int i = cursor.getCount();

		if (cursor != null) {
			cursor.close();
		}

		return i;
	}

	/**
	 * 未上传数
	 */
	@Override
	public int queryBandingNoUpload() {
		String sql = "select _id from waterinfo where bandingstatus='0'  and ismultiplecopy='0'";
		System.out.println(sql);
		Cursor cursor = db.rawQuery(sql, null);
		int i = cursor.getCount();
		if (cursor != null) {
			cursor.close();
		}

		return i;

	}

	/**
	 * 查询未上传数据
	 */
	@Override
	public List<WaterInfoVO> queryNoUpload() {
		String sql = "select w.[waternumber],w.[latitude],w.[longitude],w.[RFIDnumber],w.[adds] "
				+ "from waterinfo w where w.[bandingstatus]='0'  and w.[ismultiplecopy]='0'";
		System.out.println(sql);
		Cursor cursor = db.rawQuery(sql, null);
		List<WaterInfoVO> list = new ArrayList<WaterInfoVO>();
		while (cursor.moveToNext()) {
			WaterInfoVO water = new WaterInfoVO();
			water.setWaternumber(cursor.getString(cursor
					.getColumnIndex("waternumber")));
			water.setLatitude(cursor.getDouble(cursor
					.getColumnIndex("latitude")));
			water.setLongitude(cursor.getDouble(cursor
					.getColumnIndex("longitude")));
			water.setRFIDnumber(cursor.getString(cursor
					.getColumnIndex("RFIDnumber")));
			water.setAdds(cursor.getString(cursor.getColumnIndex("adds")));
			list.add(water);

		}
		if (cursor != null) {
			cursor.close();
		}
		return list;
	}

	/**
	 * 上传成功后更改状态
	 */
	@Override
	public boolean ModifyUploadStatus(Map<String, String> map) {
		db.beginTransaction();
		for (String key : map.keySet()) {
			ContentValues value = new ContentValues();
			value.put("bandingstatus", map.get(key));
			int i = db.update(DbTableName.WATERINFO, value,
					"waternumber=? and ismultiplecopy=?", new String[] { key,
							"0" });
			if (i < 1) {
				return false;
			}
		}
		db.setTransactionSuccessful();
		db.endTransaction();

		return true;
	}

	@Override
	public boolean CancelBanding(String waternumber) {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * 通过RFID查询用户水表信息
	 * 
	 * @param rfid
	 * @return
	 */
	public UserWaterVO queryUserWater(String rfid) {

		StringBuffer sb = new StringBuffer(
				"select w.[waternumber],w.[_id] as waterid,w.[usernumber],w.[wateradds],w.[waterproperties],w.[RFIDnumber]");
		sb.append(",w.[latitude],w.[longitude],w.[adds]");
		sb.append(",u.[username],u.[phone],u.[usertype] from waterinfo w,userinfo u where w.[usernumber]=u.[usernumber]");

		if (rfid != null || !rfid.equals("")) {
			sb.append(" and w.[RFIDnumber]='").append(rfid).append("'");
		} else {
			return null;
		}
		String sql = sb.toString();

		System.out.println(sql);
		Cursor cursor = db.rawQuery(sql, null);

		if (cursor == null) {
			return null;
		}
		UserWaterVO water = null;
		if (cursor.moveToNext()) {
			water = new UserWaterVO();
			water.setWateradds(cursor.getString(cursor
					.getColumnIndex("wateradds")));
			water.setName(cursor.getString(cursor.getColumnIndex("username")));
			water.setNumber(cursor.getString(cursor
					.getColumnIndex("usernumber")));
			water.setWaternumber(cursor.getString(cursor
					.getColumnIndex("waternumber")));
			water.setRFIDnumber(cursor.getString(cursor
					.getColumnIndex("RFIDnumber")));
			water.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
			water.setLatitude(cursor.getDouble(cursor
					.getColumnIndex("latitude")));
			water.setLongitude(cursor.getDouble(cursor
					.getColumnIndex("longitude")));
			water.setWaterid(cursor.getString(cursor.getColumnIndex("waterid")));
			water.setLocationadds(cursor.getString(cursor
					.getColumnIndex("adds")));

		}
		if (cursor != null) {
			cursor.close();
		}
		return water;

	}

	/**
	 * 上传成功后更改绑定状态
	 * 
	 * @param waterid
	 * @return
	 */
	public boolean modifyBandingRFID(String waterid) {
		boolean b = false;
		ContentValues values = new ContentValues();
		values.put("bandingstatus", "3");
		int id = db.update(DbTableName.WATERINFO, values, "_id=?",
				new String[] { waterid });
		if (id > 0) {
			b = true;
		}
		return b;
	}
}
